package com.markerhub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.markerhub.entity.*;
import com.markerhub.mapper.SysCourseMajorMapper;
import com.markerhub.service.SysCourseMajorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.markerhub.service.SysMajorService;
import com.markerhub.service.SysTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wang
 * @since 2022-04-27
 */
@Service
public class SysCourseMajorServiceImpl extends ServiceImpl<SysCourseMajorMapper, SysCourseMajor> implements SysCourseMajorService {

    @Autowired
    SysMajorService sysMajorService;

    @Override
    public Map<Long,String> majorMap(SysCourse sysCourse){
        Map<Long,String>major=new TreeMap<Long,String>();
        List<SysCourseMajor> s= this.list(new QueryWrapper<SysCourseMajor>().eq("course_id",sysCourse.getId()));
        if(s!=null) {
            for (SysCourseMajor t : s) {
                String name = sysMajorService.getOne(new QueryWrapper<SysMajor>().eq("id", t.getMajorId())).getMajor();
                major.put(t.getMajorId(), name);
            }
            return major;
        }
        else {
            return null;
        }
    }

    @Override
    public Boolean saveMajor (Long courseId,Map<Long,String> major){
        if(major==null){return false;}
        for (Long i : major.keySet()) {
            SysCourseMajor sysCourseMajor = new SysCourseMajor();
            sysCourseMajor.setCourseId(courseId);
            sysCourseMajor.setMajorId(i);
            if(!this.save(sysCourseMajor))return false;
        }
        return true;
    }

}
